﻿@{
    ViewBag.Title = "Admin Index";
}

@Html.Partial("_UserPage")
<div id="patient" class="row result">
    <div class="col-xs-3 col-sm-3 col-md-2 col-lg-2 sidebar user-sidebar">
        <ul class="nav nav-sidebar">
            <li id="FindUser" class="active">Find User</li>
            <li id="CreateUser">Create User</li>
        </ul>
    </div>
    <div id="main" class="col-xs-9 col-sm-9 col-md-10 col-lg-10 main user-content">
        @Html.Partial("~/Views/Admin/UpdateUserProfile.cshtml")
        @Html.Partial("~/Views/Admin/CreateUserProfile.cshtml")
        @Html.Partial("~/Views/Admin/FindUserProfile.cshtml")
        @Html.Partial("~/Views/Admin/FindResult.cshtml")
        @Html.Partial("~/Views/Admin/UserInfo.cshtml")
    </div>
</div>
@Html.Partial("_Message")
@Html.Partial("~/Views/Admin/UserInfo.cshtml")
@section Scripts {
    <script>
        var home = '@Url.Action("", "Home")';
        var userType = '@User.UserType';
        var DayArrays = [
        ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10',
        '11', '12', '13', '14', '15', '16', '17', '18', '19', '20',
        '21', '22', '23', '24', '25', '26', '27', '28'],
        ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10',
        '11', '12', '13', '14', '15', '16', '17', '18', '19', '20',
        '21', '22', '23', '24', '25', '26', '27', '28', '29'],
        ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10',
        '11', '12', '13', '14', '15', '16', '17', '18', '19', '20',
        '21', '22', '23', '24', '25', '26', '27', '28', '29', '30'],
        ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10',
        '11', '12', '13', '14', '15', '16', '17', '18', '19', '20',
        '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31']
        ];
        var MonthDay = {
            1: 31,
            3: 31,
            5: 31,
            7: 31,
            8: 31,
            10: 31,
            12: 31,
            4: 30,
            6: 30,
            9: 30,
            11: 30
        };
    </script>
    @Scripts.Render("~/bundles/message")
    @Scripts.Render("~/Scripts/ContentHeightController.js")
    @Scripts.Render("~/bundles/knockout")
    @Scripts.Render("~/bundles/my_knockout")
    @Scripts.Render("~/Scripts/ViewModel/FindUserProfileVM.js")
    @Scripts.Render("~/Scripts/jquery.printArea.js")
    @Scripts.Render("~/Scripts/ViewModel/UserProfileCreateVM.js")
    @Scripts.Render("~/Scripts/ViewModel/UserProfileUpdateVM.js")
    <script>
        $(function () {
            ko.applyBindings(findUserVM, document.getElementById("findUserVM"));
            ko.applyBindings(createUserProfileVM, document.getElementById("CreateUserProfileForm"));
            ko.applyBindings(updateUserProfileVM, document.getElementById("UpdateUserProfileForm"));
            var indexController = {
                tab: 1,
                message: messageController,
                searchTerm: $("#searchTerm"),
                btnSearch: $("#btnSearch"),
                enableSearch: function (state) {
                    var p = this;
                    if (state == true) {
                        this.btnSearch.on("click", function () {
                            var searchType;
                            if ($("#searchPatientID").prop("checked") == true) {
                                searchType = 1;
                            }
                            else {
                                searchType = 2;
                            }
                            findUserVM.loadPatientInfo(p.searchTerm.prop("value"), searchType, indexController);
                        });
                        this.searchTerm.on("keypress", function (event) {
                            if (event.which == 13) {//Enter
                                p.btnSearch.trigger("click");
                            }
                        });
                    }
                    else {
                        p.btnSearch.off("click");
                        p.searchTerm.off("keypress");
                    }
                },

                hideResult: function(){
                    var p = this;
                    if (p.FindResultForm.is(":visible")) {
                        p.FindResultForm.hide();
                    }
                    if (p.UserInfoForm.is(":visible")) {
                        p.UserInfoForm.hide();
                    }
                },

                FindUser: $("#FindUser"),
                CreateUser: $("#CreateUser"),
                FindUserProfileForm: $("#FindUserProfileForm"),
                CreateUserProfileForm: $("#CreateUserProfileForm"),
                displayFindUserProfileForm: function (state) {
                    var p = this;
                    if (state == true) {
                        p.FindUserProfileForm.show();
                        p.searchTerm.focus();
                        p.FindUser.addClass("active");
                        p.tab = 1;
                    }
                    else {
                        p.FindUserProfileForm.hide();
                        p.FindUser.removeClass("active");
                    }
                },
                //namNhuan: function(year){
                //    if(year%400==0 || (year%4==0 && year%100!=0)){
                //        return true;
                //    }
                //    return false;
                //},
                displayCreateUserProfileForm: function (state) {
                    var p = this;
                    if (state == true) {
                        p.tab = 2;
                        p.CreateUserProfileForm.show();
                        $("#CreateUserProfileForm #SocialID").focus();
                        p.CreateUser.addClass("active");
                        $("#CreateUserProfileYear").on("keyup", function () {
                            if (createUserProfileVM.validYear() == false) {
                                createUserProfileVM.BirthdayException(messageController.messageData["invalid_year"].text);
                            }
                            else {
                                createUserProfileVM.BirthdayException('');
                                //if (createUserProfileVM.SelectedMonth() == 2) {
                                //    if (indexController.namNhuan(("#CreateUserProfileYear").val()) == true) {
                                //        createUserProfileVM.Days(DayArrays[1]);
                                //    }
                                //    else {
                                //        createUserProfileVM.Days(DayArrays[0]);
                                //    }
                                //    createUserProfileVM.SelectedDay('');
                                //}
                            }
                        });
                        $("#CreateUserProfileForm #Phone").on("keyup", function () {
                            if (createUserProfileVM.validNumberOrEmpty($(this).val()) == false) {
                                createUserProfileVM.PhoneException(messageController.messageData["invalid_phone"].text);
                            }
                            else {
                                createUserProfileVM.PhoneException('');
                            }
                        });
                    }
                    else {
                        p.CreateUserProfileForm.hide();
                        p.CreateUser.removeClass("active");
                        $("#CreateUserProfileYear").off("keyup");
                        $("#CreateUserProfileForm #Phone").off("keyup");
                    }
                },
                activeSideBar: function (state) {
                    var p = this;
                    if (state == true) {
                        p.FindUser.on("click", function () {
                            if (!p.FindUserProfileForm.is(":visible")) {
                                createUserProfileVM.cancelLoadRequest();
                                p.displayFindUserProfileForm(true);
                                p.hideResult();
                                p.displayCreateUserProfileForm(false);
                            }
                        });
                        p.CreateUser.on("click", function () {
                            if (!p.CreateUserProfileForm.is(":visible")) {
                                findUserVM.cancelLoadRequest();
                                p.displayFindUserProfileForm(false);
                                p.displayCreateUserProfileForm(true);
                                p.hideResult();
                                createUserProfileVM.loadData(indexController);
                            }
                        });
                    }
                    else {
                        p.FindUser.off("click");
                        p.CreateUser.off("click");
                    }
                },

                UserInfoForm: $("#UserInfoForm"),
                FindResultForm: $("#FindResultForm"),
                displayUserInfoForm: function (state) {
                    var p = this;
                    if (state == true) {
                        p.UserInfoForm.show();
                    }
                    else {
                        p.UserInfoForm.hide();
                    }
                },
                displayFindResultForm: function (state) {
                    var p = this;
                    if (state == true) {
                        p.FindResultForm.show();
                    }
                    else {
                        p.FindResultForm.hide();
                    }
                },
                printerLoading: function () {
                    indexController.message.processing("printer_loading");
                    indexController.message.processComplete("printer_wait");
                },
                printUserInfo: function () {
                    $("#printUserInfo").printArea();
                },
                btnPrintInfo: $("#print-info"),
                enableBtnPrintInfo: function (state) {
                    var p = this;
                    if (state == true) {
                        p.btnPrintInfo.on("click", function () {
                            p.printUserInfo();
                            p.printerLoading();
                        });
                    }
                    else {
                        p.btnPrintInfo.off();
                    }
                },
                btnEditInfo: $("#edit-info"),
                enableBtnEditInfo: function (state) {
                    var p = this;
                    if (state == true) {
                        p.btnEditInfo.on("click", function () {
                            p.displayUpdateUserProfileForm(true);
                            updateUserProfileVM.loadData(findUserVM.ID(), indexController);
                        });
                    }
                    else {
                        p.btnEditInfo.off();
                    }
                },
                btnResetPassword: $("#reset-password"),
                enableBtnResetPassword: function (state) {
                    var p = this;
                    if (state == true) {
                        p.btnResetPassword.on("click", function () {
                            findUserVM.resetPassword(indexController);
                        });
                    }
                    else {
                        p.btnResetPassword.off();
                    }
                },
                enableUserInfoButton: function (state) {
                    var p = this;
                    if (state == true) {
                        p.enableBtnPrintInfo(true);
                        p.enableBtnEditInfo(true);
                        p.enableBtnResetPassword(true);
                    }
                    else {
                        p.enableBtnPrintInfo(false);
                        p.enableBtnEditInfo(false);
                        p.enableBtnResetPassword(false);
                    }
                },
                btnCreateUserCreate: $("#create-user-create"),
                enableBtnCreateUserCreate: function (state) {
                    var p = this;
                    if (state == true) {
                        p.btnCreateUserCreate.on("click", function () {
                            createUserProfileVM.createUser(indexController);
                        });
                    }
                    else {
                        p.btnCreateUserCreate.off("click");
                    }
                },
                btnCreateUserClear: $("#create-user-clear"),
                enableBtnCreateUserClear: function (state) {
                    var p = this;
                    if (state == true) {
                        p.btnCreateUserClear.on("click", function () {
                            createUserProfileVM.clearOldData();
                        });
                    }
                    else {
                        p.btnCreateUserClear.off("click");
                    }
                },
                btnCreateWithSocialID: $("#create-with-socialid"),
                enableBtnCreateWithSocialID: function (state) {
                    var p = this;
                    if (state == true) {
                        p.btnCreateWithSocialID.on("click", function () {
                            p.CreateUser.trigger("click");
                            createUserProfileVM.SocialID(p.searchTerm.prop("value"));
                        });
                    }
                    else {
                        p.btnCreateWithSocialID.off("click");
                    }
                },


                UpdateUserProfileForm: $("#UpdateUserProfileForm"),
                displayUpdateUserProfileForm: function (state) {
                    var p = this;
                    if (state == true) {
                        p.UpdateUserProfileForm.show();
                        p.enableBtnUpdateUserCancel(true);
                        $("#UpdateUserProfileYear").on("keyup", function () {
                            if (updateUserProfileVM.validYear() == false) {
                                updateUserProfileVM.BirthdayException(messageController.messageData["invalid_year"].text);
                            }
                            else {
                                updateUserProfileVM.BirthdayException('');
                            }
                        });
                        $("#UpdateUserProfileForm #Phone").on("keyup", function () {
                            if (updateUserProfileVM.validNumberOrEmpty($(this).val()) == false) {
                                updateUserProfileVM.PhoneException(messageController.messageData["invalid_phone"].text);
                            }
                            else {
                                updateUserProfileVM.PhoneException('');
                            }
                        });
                        indexController.activeSideBar(false);
                        p.displayFindUserProfileForm(false);
                        p.displayCreateUserProfileForm(false);
                        p.displayUserInfoForm(false);
                    }
                    else {
                        p.UpdateUserProfileForm.hide();
                        p.enableBtnUpdateUserCancel(false);
                        $("#UpdateUserProfileYear").off("keyup");
                        $("#UpdateUserProfileForm #Phone").off("keyup");
                        indexController.activeSideBar(true);
                        switch (p.tab) {
                            case 1:
                                p.displayFindUserProfileForm(true);
                                break;
                            case 2:
                                p.displayCreateUserProfileForm(true);
                                break;
                        }
                        if (findUserVM.ID()) {
                            p.displayUserInfoForm(true);
                        }
                    }
                },
                btnUpdateUserUpdate: $("#update-user-update"),
                enableBtnUpdateUserUpdate: function (state) {
                    var p = this;
                    if (state == true) {
                        p.btnUpdateUserUpdate.on("click", function () {
                            updateUserProfileVM.updateUser(indexController);
                        });
                    }
                    else {
                        p.btnUpdateUserUpdate.off("click");
                    }
                },
                btnUpdateUserCancel: $("#update-user-cancel"),
                enableBtnUpdateUserCancel: function (state) {
                    var p = this;
                    if (state == true) {
                        p.btnUpdateUserCancel.on("click", function () {
                            p.displayUpdateUserProfileForm(false);
                            updateUserProfileVM.cancelLoadRequest();
                        });
                    }
                    else {
                        p.btnUpdateUserCancel.off("click");
                    }
                },
            };
            indexController.displayFindUserProfileForm(true);
            indexController.enableSearch(true);
            contentHeightController.setContentHeight();
            contentHeightController.activeChangeContentHeight();
            indexController.activeSideBar(true);
            indexController.enableUserInfoButton(true);
            indexController.enableBtnCreateWithSocialID(true);
        });
    </script>
}
